package code
{
	import event.TriviaEvent;
	
	import flash.events.MouseEvent;
	
	import model.ApplicationModel;
	
	import mx.containers.Panel;
	import mx.events.FlexEvent;
	import mx.skins.spark.PanelBorderSkin;
	
	import spark.components.BorderContainer;
	
	import view.ShowResultView;
	
	public class ShowResultCB extends BorderContainer
	{
		private var _view:ShowResultView; 
		private var _aplication:Main; 
		[Bindable]
		public var _applicationModel : ApplicationModel = ApplicationModel.instance;
		
		public function ShowResultCB()
		{
			_view = this as ShowResultView;
			_view.addEventListener(FlexEvent.CREATION_COMPLETE, doInit);
		}
		
		
		public function doInit(event:FlexEvent):void
		{
			_aplication = _view.parentApplication as Main;
			_view.btStartTrivia.addEventListener(MouseEvent.CLICK,startTrivia,false,0,false);
			//_view.btSendTrivia.addEventListener(MouseEvent.CLICK,sendTrivia,false,0,false);
			displayClockCount();
		}
		private function displayClockCount():void
		{
			try {
				var dif:int =  _applicationModel.clockCounter.currentCount;
				if (dif>60)
				{
					_view.tfTimeDisplay.text = "Tiempo:"+ String(Math.round(Math.pow(10,2)*(dif/60))/Math.pow(10,2))+' minutos';
				}else
				{
					_view.tfTimeDisplay.text = "Tiempo:"+ String(dif)+' segundos';
				}
			}catch(e:Error)
			{
			}
		}
		public function sendTrivia(event:Event=null):void
		{
			(new TriviaEvent(TriviaEvent.TRIVIA_SEND_SOLUTIONS)).dispatch();
			(new TriviaEvent(TriviaEvent.TRIVIA_EVENT_SAVE_STAT)).dispatch();
		}
		public function startTrivia (event:Event):void
		{
			_aplication.currentState = 	"stateStarTrivia";
			_applicationModel.reStart();
			
		}
		
	}
}